﻿$(document).ready(function () {

    // Add slide effect to ImageVault images on start page
    $('#slider').slides({
        preload: true,
        preloadImage: '/images/loading.gif',
        play: 5000,
        pause: 2500,
        slideSpeed: 1000,
        generatePagination: false,
        slideEasing: 'easeInOutExpo',
        container: 'slides-container',
        hoverPause: true,
        animationStart: function (current) {
            $('.slides-container .slide').eq(current - 1).find('.caption p').fadeTo(100, 0);
            var caption = $('.slides-container .slide').eq(current).find('.caption p');
            caption.fadeTo(0, 0);
        },
        animationComplete: function (current) {
            var caption = $('.slides-container .slide').eq(current - 1).find('.caption p');
            var finish = caption.width();
            caption.children().hide();
            caption.width(0);
            caption.fadeTo(0, 1).animate({ width: finish + 'px' }, 1000, 'easeInOutExpo', function () {
                caption.children().fadeTo(500, 1);
            });
        }
    });

    // Add full size image
    $('#images a').hover(function (e) {
        var link = $('<img src=\"/images/fullsize-icon.png\" class=\"fullsize\" alt=\"\" />');
        link.hide();
        $(this).append(link);
        link.fadeIn();
    }, function (e) {
        $('img.fullsize').fadeOut('fast').remove();
    });

    // Add lightbox to imagevault gallery
    $('#images a').lightweightbox();

    $('#image-search').labelize();

    // Add show/hide functionality to the scope bar categories
    var $categories = $('#scope-bar form div div');

    // Add upload functionality

    $('#upload-link').click(function () {
        var $container = $('<div id="uploader"><div></div></div>');
        $('body').append($container);
        $('#uploader div').click(function (e) { e.stopPropagation() });
        $('#uploader').click(function () {
            $(this).fadeOut();
            $(this).remove();
        });
        // Setup html5 version
        $("#uploader div").pluploadQueue({
            // General settings
            runtimes: 'html5,flash',
            url: '/Services/UploadService.asmx/Upload',
            max_file_size: '100mb',
            chunk_size: '100mb',
            unique_names: true,
            dragdrop: true,

            // Specify what files to browse for
            filters: [{ title: "Image files", extensions: "jpg,gif,png" }, { title: "Zip files", extensions: "zip"}],

            // Flash settings
            flash_swf_url: '/scripts/plupload.flash.swf',

            // Silverlight settings
            silverlight_xap_url: '/scripts/plupload.silverlight.xap'
        });

    });

    var selectCategories = $('html').attr('lang') == 'sv' ? 'Visa Kategorier' : 'Select categories';
    var hideCategories = $('html').attr('lang') == 'sv' ? 'Dölj Kategorier' : 'Hide categories';

    var $categorieslink = $('<span><a href="/">' + selectCategories + '</a></span>');
    if ($categories.find('input:checked').length > 0) {
        $categorieslink.find('a').append('<em> *</em>');
    }
    else {
        $categorieslink.find('em').remove();
    }
    // Add a link for closing the categories
    var $closelink = $('<span><a href="/">' + hideCategories + '</a></span>');
    $categories.hide().prepend($closelink).before($categorieslink);
    $categorieslink.click(function () {
        $categories.click(function (e) { e.stopPropagation() });
        $('html').live('click', closeCategories);
        $categorieslink.hide();
        $categories.show();
        return false;
    });
    // Setup event for closing categories
    $closelink.click(closeCategories);
    function closeCategories() {
        $categories.fadeOut('fast');
        $categorieslink.show();
        if ($categories.find('input:checked').length > 0 && $categorieslink.find('em').length == 0) {
            $categorieslink.find('a').append('<em> *</em>');
        }
        if ($categories.find('input:checked').length == 0) {
            $categorieslink.find('em').remove();
        }
        $('html').die('click');
        return false;
    }

    // Find all anchors with the class popup and add the onclick function to open the link in a new window.
    $('a.popup').click(function () {
        window.open($(this).attr('href'));
        return false;
    });
    // Traverse through all links with the class popup and modify the title attribute.
    $('a[class*="popup"]').each(function () {
        $(this).attr('title', $(this).attr('title') + ' (Opens in new window)');
    });
    // Find all divs with with answers and hide them
    $('div.answer').hide();

    $('#query').labelize();

    // Google Translate
    $('.google-translate').translate({ imageUrl: '/images/google-translate.png' });
    $('.translate').click(function () {
        if ($('#translate-dialog').length == 0)
            OpenTranslate(this)
        else
            $('#translate-dialog').remove();
        return false;
    });

    //Set focus to input field when displaying comment form
    $('#comment-form #name').focus();
});